<template>
  <div class="app-container home">
    <el-card class="box-card"   shadow="never">
      <el-row>
        <el-col :span="8">
            <!-- 显示在线列表数-->
            <el-row>
              <el-col :span="16">
              <!-- {{deviceNum}} -->
                <div class="number">当前设备数量:10<span></span></div>
              </el-col>
              <el-col :span="8" style="text-align: center;">
                <el-button type="primary" icon="el-icon-refresh" @click="refreshDevice()"  size="mini"></el-button>
              </el-col>
            </el-row>

            <!--设备列表 -->
            <el-table ref="singleTable" :data="equipmentdata"  style="width: 90%;margin-top: 15px;"  highlight-current-row  @row-click="test" >
              <el-table-column  prop="deviceId" label="设备编号" > </el-table-column>
              <el-table-column  prop="deviceCode" label="设备编码"></el-table-column>
              <el-table-column  prop="status" label="绑定状态" >
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.device_binding_status" :value="scope.row.status"/>
                </template>
              </el-table-column>
            </el-table>
        </el-col>

        <el-col :span="16">
          <!-- <el-row>
            <el-form :model="queryParams" ref="queryForm" size="small" :inline="true"  label-width="68px">
              <el-form-item label="用户名称">
                <el-input
                  v-model="queryParams.userName"
                  placeholder="请输入用户名称"
                  clearable
                  style="width: 240px"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="设备编码">
                <el-input
                  v-model="queryParams.deviceCode"
                  placeholder="请输入设备编码"
                  clearable
                  style="width: 240px"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="状态" prop="state">
                <el-select
                  v-model="queryParams.state"
                  placeholder="用户状态"
                  clearable
                  style="width: 240px"
                >
                  <el-option label="未绑定" value="0"
                  />
                  <el-option label="已绑定" value="1"/>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="query()">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="reset()" >重置</el-button>
              </el-form-item>
            </el-form>
          </el-row> -->
          <el-row>
            <el-button type="primary" icon="el-icon-position" size="mini" @click="binding()">绑定</el-button>
            <!-- 添加或修改菜单对话框 -->
            <el-dialog title="绑定设备" :visible.sync="dialogVisible" width="680px" :before-close="handleClose" append-to-body>
              <el-form ref="" :model="insertbind" label-width="100px">
                <el-row>
                  <el-col :span="12">
                    <el-form-item label="设备编号" >
                      <el-select v-model="insertbind.deviceId" placeholder="请选择设备编号">
                        <el-option v-for="(item, index) in unbindequipment" :key="index" :label="item.deviceCode" :value="item.deviceId"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="用户名称" >
                      <el-select v-model="insertbind.roleId" placeholder="请选择要绑定的用户">
                        <el-option v-for="(item, index) in userList" :key="index" :label="item.userName" :value="item.userId"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button type="primary"  @click="submitBind()">确 定</el-button>
                <el-button @click="cancel()">取 消</el-button>
              </div>
            </el-dialog>
          </el-row>
          <el-row>
            <el-table ref="singleTable" :data="bindingdata"  style="width: 90%;margin-top: 15px;"  highlight-current-row>
              <el-table-column type="selection" width="55"/>
              <el-table-column  prop="name" label="用户名称" > </el-table-column>
              <el-table-column  prop="deviceCode" label="设备编码" > </el-table-column>
              <el-table-column  prop="status" label="绑定状态" >
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.device_binding_status" :value="scope.row.status"/>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template  slot-scope="scope">
                  <el-button v-if="scope.row.status==0"
                    size="mini"
                    type="text"
                    icon="el-icon-circle-close"
                    v-hasPermi="['system:role:remove']"
                    @click="unbinding(scope.row)"
                  >取消授权
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-row>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import { listApi, listApi2, unbind, findunbind,bindevice } from '@/api/hat/device.js'
import { unbindDeviceUser} from "@/api/system/user"
export default {
  name: 'binding',
  dicts: ['device_binding_status'],
  data() {
    return {
      //设备表
      equipmentdata: [],
      //绑定设备表
      bindingdata: [],
      //查询参数
      queryParams: {
        pageSize:1,
        pageNum:10,
      },
      dialogVisible: false,
      //绑定设备表单
      insertbind: {},
      //未绑定设备表
      unbindequipment: [],
      //用户表
      userList: [],
    }
  },
  created() {
    this.getEquipMentdata()
    //this.query()
  },
  methods: {
    getEquipMentdata () {
      listApi2(this.queryParams).then(resp => {
        this.equipmentdata = resp.data.list
      })
    },
    //点左边表格，在右边显示详细信息
    test (val) {
      this.queryParams.deviceCode=val.deviceCode
      this.query()
    },
    query (){
      listApi2(this.queryParams).then(resp => {
        // console.log(resp.data)
        this.bindingdata = resp.data.list
      })
    },
    unbinding (val) {
      // console.log(val)
      unbind(val.id).then(resp => {
        this.$message({
          message: '解绑成功！',
          type: 'success'
        });
        this.queryParams = {
        pageSize:1,
        pageNum:10,
      }
        this.getEquipMentdata()
        //两个表同时更新
        this.bindingdata=[]
      })
    },
    binding () {
      this.dialogVisible = true
      findunbind().then(resp => {
        console.log(resp.data)
        this.unbindequipment=resp.data
      }),
      unbindDeviceUser().then(response => {
        console.log("用户：",response.data)
          this.userList = response.data;
        }
      );
    },
    reset () {
      this.queryParams = {
        pageSize:1,
        pageNum:10,
      }
      this.getEquipMentdata()
    }
    ,
    handleClose (done) {
      this.cancel()
    }
    ,
    cancel () {
      this.dialogVisible = false
      this.insertbind={}
    }
    ,
    submitBind () {
      this.$confirm('确认将该设备绑定吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).then(() => {
        bindevice(this.insertbind).then()
        this.$message({
          type: 'success',
          message: '绑定成功!'
        })
        this.dialogVisible = false
        //重置表单
       this.reset()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消绑定'
          });          
        })
    }
  }
}
</script>

<style scoped>

</style>